package com.book.controller;

import com.book.common.BaseController;
import com.book.common.NoRepeatSubmit;
import com.book.dto.AddressDto;
import com.book.remote.AddressRemote;
import com.book.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @version 1.0
 * @Author Jincheng.Li
 * @Date 2021/5/19 5:20 下午
 * @Project_Name graduation-project
 */
@Slf4j
@Controller
@RequestMapping("/address")
public class AddressController extends BaseController {
    @Autowired
    private AddressRemote addressRemote;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseVo list() {
        try {
            String uCode = getUserCode();
            log.info("AddressController.list#uCode:{}", uCode);
            List<AddressDto> list = addressRemote.getList(uCode);
            log.info("AddressController.list#list:{}", list);
            if (CollectionUtils.isEmpty(list)) {
                return ResponseVo.errRest("查询失败");
            }
            return ResponseVo.successResult(list);
        } catch (Exception e) {
            log.error("AddressController.list异常", e);
            return ResponseVo.errRest("查询失败");
        }
    }

    @RequestMapping("/getAddress")
    @ResponseBody
    public ResponseVo getAddressById(Integer id) {
        AddressDto addressDto = addressRemote.getAddressById(id);
        log.info("AddressController.getAddressById#id:{},addr:{}", id, addressDto);
        return addressDto == null ? ResponseVo.errRest("没有数据") : ResponseVo.successResult(addressDto);
    }

    @NoRepeatSubmit
    @RequestMapping("/editAddress")
    @ResponseBody
    public ResponseVo editAddress(AddressDto addressDto) {
        addressDto.setUCode(getUserCode());
        Boolean aBoolean = addressRemote.editAddress(addressDto);
        return aBoolean ? ResponseVo.successResultNotData() : ResponseVo.errRest("更新失败");
    }

    @RequestMapping("/addressInfo")
    public String addressInfo(Model model){
        List<AddressDto> list = addressRemote.getList(getUserCode());
        model.addAttribute("addrList",list);
        return "/address/info.jsp";
    }

    @RequestMapping("/addPage")
    public String addPage(){
        return "/address/addAddr.jsp";
    }

    @NoRepeatSubmit
    @RequestMapping("/addAddress")
    @ResponseBody
    public ResponseVo add(AddressDto addressDto){
        String uCode = getUserCode();
        addressDto.setUCode(uCode);
        Boolean aBoolean = addressRemote.addAddress(addressDto);
        return aBoolean?ResponseVo.successResultNotData():ResponseVo.errRest("添加失败");
    }

    @RequestMapping("/delete/${id}")
    @ResponseBody
    public ResponseVo delete(@PathVariable Integer id){
        Boolean aBoolean= addressRemote.delete(id);
        return aBoolean?ResponseVo.successResultNotData():ResponseVo.errRest("删除失败");
    }
}
